package se.webbzon.boltzmann.object.background;

import java.awt.Color;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

import se.webbzon.boltzmann.environment.AbstractEnvironment;
import se.webbzon.oschi01.friendly.Friendly;
import se.webbzon.oschi01.worldeditor.WorldObjectDeflation;
import se.webbzon.oschi01.worldobject.WorldObject;

public class BackgroundDeflation extends WorldObjectDeflation {
	
	private String backgroundName;
	
	private double width, height, panningMax, tiltMax;
	
	private Color color;
	
	/*============================================================
	Constructors
	============================================================*/

	/** Creates a new empty background deflation. **/
	public BackgroundDeflation(	int deflationProperties,
								WorldObject[] dependencies) {
		super(deflationProperties, dependencies);
	}
	
	/** Creates a new background deflation from the given
	 * background. **/
	public BackgroundDeflation(	int deflationProperties,
								Background background) {
		super(deflationProperties, null);
		this.backgroundName = background.getBackgroundName();
		this.width = background.getWidth();
		this.height = background.getHeight();
		this.panningMax = background.getPanningMax();
		this.tiltMax = background.getTiltMax();
		this.color = background.getColor();
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public void inflate(ByteBuffer buff) {
		// Read from buffer
		width = buff.getDouble();
		height = buff.getDouble();
		panningMax = buff.getDouble();
		tiltMax = buff.getDouble();
		final int red, green, blue, alpha;
		red = 0x000000FF & buff.get();
		green = 0x000000FF & buff.get();
		blue = 0x000000FF & buff.get();
		alpha = 0x000000FF & buff.get();
		color = new Color(red,green,blue,alpha);
		byte[] backgroundNameData = new byte[buff.getShort()];
		buff.get(backgroundNameData, 0, backgroundNameData.length);
		try {
			backgroundName = new String(backgroundNameData,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			backgroundName = "";
		}
	}

	@Override public ByteBuffer deflate(int allocate) {
		byte[] backgroundNameData;
		try {
			backgroundNameData = backgroundName.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
			backgroundNameData = new byte[0];
		}
		short size = (short) backgroundNameData.length;
		ByteBuffer buff = ByteBuffer.allocate(allocate + 38 + size);
		
		// Write to buffer
		buff.putDouble(width);
		buff.putDouble(height);
		buff.putDouble(panningMax);
		buff.putDouble(tiltMax);
		buff.put((byte) color.getRed());
		buff.put((byte) color.getGreen());
		buff.put((byte) color.getBlue());
		buff.put((byte) color.getAlpha());
		buff.putShort(size);
		buff.put(backgroundNameData, 0, size);
		
		return buff;
	}

	/*============================================================
	Friendly Methods
	============================================================*/
	
	/** Creates a new background from this background deflation. **/
	@Friendly Background makeBackground(AbstractEnvironment env) {
		return new Background(	env,backgroundName,color,
								width,height,panningMax,tiltMax);
	}
	
}
